﻿using System;
using System.Net.Http.Formatting;
using System.Web.Http;
using System.Web.Mvc;
using Monitor.Controllers.ApiControllers;
using Monitor.Models.Core;
using Monitor.Models.JsonModel;

namespace Monitor.Controllers
{
    public class HomeController : MainController
    {

       #region Index
        [System.Web.Mvc.AllowAnonymous]
        public ActionResult Index()
            {
                return View();
            }        
        #endregion

       #region MapView
        
        public ActionResult MapView()
        {
            return View();
        }
        #endregion

       #region Report
       [System.Web.Mvc.HttpGet]
       public ActionResult Report()
       {
           return View();
       }

       [System.Web.Mvc.HttpGet]
       public ActionResult GetReports()
       {
           try
           {
               if (TempData.ContainsKey("ReportModel"))
               {
                   var model = TempData["ReportModel"];
                   if ((model is GetReport) && (model != null))
                   {
                       ReportCreater report = new ReportCreater(model as GetReport, ErrorLogger);

                       return File(report.GetReport.GetAsByteArray(),
                                   "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                                   report.Name + ".xlsx"
                           );
                   }
               }
           }
           catch (Exception exception)
           {
               ErrorLogger.Error("HomeController - GetReports", exception);
           }
           return View("Error");
       }

       [System.Web.Mvc.HttpPost]
       public ActionResult GetReports(GetReport model)
       {
           if (ModelState.IsValid)
           {
               TempData["ReportModel"] = model;
               return Json(true);
           }
           return Json(false);
       }



       #endregion

       #region ArchiveState

       [System.Web.Mvc.HttpGet]
       public ActionResult ArchiveState()
       {
           return View();
       }
        #endregion

    }
}
